/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：AdvCfgController.java
 * History:
 *         2012-6-29: Initially created, Chrise.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.base.SystemContent;
import com.xteam.ecsystem.entities.SysAdvConfig;
import com.xteam.ecsystem.service.admin.AdvCfgService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 广告配置控制层。
 * @author Chrise
 */
@Controller
public class AdvCfgController {
	
	@Autowired
	private AdvCfgService acService;
	
	@Autowired
	private SystemLogService logService;
	
	/**
	 * 获取广告配置。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getAdvConfigs.do")
	public void getAdvConfigs(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int sacType = ReqHelper.getRequestInt(request, "sacType");
		String sacName = ReqHelper.getRequestString(request, "sacName");
		int sacPage = ReqHelper.getRequestInt(request, "sacPage");
		int sacPosition = ReqHelper.getRequestInt(request, "sacPosition");
		int sacState = ReqHelper.getRequestInt(request, "sacState");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		PageCenterStation<SysAdvConfig> pcs = this.acService.getAdvConfigs(sacType, sacName, 
			sacPage, sacPosition, sacState, pageIndex, pageSize);
		JSONObject result = JSONObject.fromObject(pcs);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 切换广告配置状态。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/switchAcState.do")
	public void switchAcState(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONObject object = JSONObject.fromObject(data);
		
		this.acService.switchState(object.getString("sacId"), object.getBoolean("sacTo"));
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ADVCFG_MGR, 
				"switchState", ConstantLocation.LOG_Edit_STATE, "SysAdvConfig");
	}
	
	/**
	 * 删除广告配置。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/delAdvConfigs.do")
	public void delAdvConfigs(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		
		JSONArray array = JSONArray.fromObject(data);
		
		List<String> ids = new ArrayList<String>();
		for (int index = 0; index < array.size(); index ++) {
			JSONObject object = array.getJSONObject(index);
			ids.add(object.getString("sacId"));
		}
		
		this.acService.delAdvConfigs(ids);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ADVCFG_MGR, 
				"delAdvConfigs", ConstantLocation.LOG_DELETE_CFG, "SysAdvConfig");
	}
	
	/**
	 * 保存广告配置。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/saveAdvConfig.do")
	public void saveAdvConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD_CFG;
		
		JSONObject object = JSONObject.fromObject(data);
		SysAdvConfig sac = (SysAdvConfig)JSONObject.toBean(object, SysAdvConfig.class);
		
		JSONObject result = new JSONObject();
		if (this.acService.saveAdvConfig(sac)) {
			result.put("issuccess", true);
			
			if(StringUtil.isNotNullOrEmpty(object.get("sacId"))){
				lslType = ConstantLocation.LOG_EDIT_CFG;
			}
			
			this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ADVCFG_MGR, 
					"saveAdvConfig", lslType, "SysAdvConfig");
		} else {
			result.put("issuccess", false);
			result.put("message", "已存在相同显示名称的配置！");
		}
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取广告配置。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getAdvConfig.do")
	public void getAdvConfig(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String data = ReqHelper.getRequestString(request, "data");
		JSONObject object = JSONObject.fromObject(data);
		
		SysAdvConfig sac = this.acService.getAdvConfig(object.getString("sacId"));
		
		JSONObject result = new JSONObject();
		if (sac != null) {
			JSONObject bean = JSONObject.fromObject(sac);
			
			result.put("issuccess", true);
			result.put("message", bean.toString());
		} else {
			result.put("issuccess", false);
			result.put("message", "指定配置不存在！");
		}
		
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取广告内容。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getAdvContent.do")
	public void getAdvContent(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int type = ReqHelper.getRequestInt(request, "type");
		
		List<SystemContent> list = this.acService.getAdvContent(type);
		JSONArray result = JSONArray.fromObject(list);
		response.getWriter().write(result.toString());
	}
	
	/**
	 * 获取广告内容。
	 * @author Chrise 2012-6-29
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getAdvContents.do")
	public void getAdvContents(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int scType = ReqHelper.getRequestInt(request, "scType");
		String scName = ReqHelper.getRequestString(request, "scName");
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		
		PageCenterStation<SystemContent> pcs = this.acService.getAdvContents(scType, scName, pageIndex, pageSize);
		JSONObject result = JSONObject.fromObject(pcs);
		response.getWriter().write(result.toString());
	}
}
